package com.jingdianjichi.subject.application.controller;

import com.jingdianjichi.subject.application.converter.SubjectLabelDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectLabelDTO;
import com.jingdianjichi.subject.common.entity.Result;
import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBO;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/*
* 标签controller
* */
@RestController
@Slf4j
@RequestMapping("/subject/label")
public class SubjectLabelController {

    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    /*
    * 新增标签
    * */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.add.subjectLabelDTO:{}",subjectLabelDTO);
            }

            Objects.requireNonNull(subjectLabelDTO.getLabelName(),"标签名字不能为空");
            Objects.requireNonNull(subjectLabelDTO.getCategoryId(),"标签的分类id不能为空");
            Objects.requireNonNull(subjectLabelDTO.getSortNum(),"排序不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.
                    Instance.LabelDTOconveeterBO(subjectLabelDTO);
            Boolean result = subjectLabelDomainService.add(subjectLabelBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("SubjectLabelController.add:{}",e.getMessage(),e);
            return Result.fail("新增标签失败");
        }
    }

    /*
    *更新标签
    * */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.update.subjectLabelDTO:{}", subjectLabelDTO);
            }

            Objects.requireNonNull(subjectLabelDTO.getId(),"标签的id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.
                    Instance.LabelDTOconveeterBO(subjectLabelDTO);
            Boolean update = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(update);
        } catch (Exception e) {
            log.error("SubjectLabelController.update:{}",e.getMessage(),e);
            return Result.fail("更新标签失败");
        }
    }

    /*
    * 删除标签
    * */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.delete.subjectLabelDTO:{}",subjectLabelDTO);
            }

            Objects.requireNonNull(subjectLabelDTO.getId(),"标签的id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.
                    Instance.LabelDTOconveeterBO(subjectLabelDTO);

            subjectLabelBO.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
            Boolean delete = subjectLabelDomainService.update(subjectLabelBO);
            return Result.ok(delete);
        } catch (Exception e) {
            log.error("SubjectLabelController.delete:{}",e.getMessage(),e);
            return Result.fail("删除失败");
        }
    }

    /*
    * 根据分类查询标签
    * */
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SubjectLabelController.queryLabelByCategoryId.subjectLabelDTO:{}",subjectLabelDTO);
            }

            Objects.requireNonNull(subjectLabelDTO.getCategoryId(),"分类的id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelDTOConverter.
                    Instance.LabelDTOconveeterBO(subjectLabelDTO);
            List<SubjectLabelBO> boList = subjectLabelDomainService.queryLabelByCategory(subjectLabelBO);

            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelDTOConverter.
                    Instance.BOconverterDTO(boList);

            return Result.ok(subjectLabelDTOS);
        } catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId:{}",e.getMessage(),e);
            return Result.fail("根据分类查询标签失败");
        }
    }
}
